%% Horizontal alignment of tables, graphs, matrices, arrays
% by Jaromir Benes
% 
% In this m-file, we make tables, graphs, and matices aligned horizontally,
% and show how to create shared titles and subtitles.

%% Clear Workspace

clear;
close all;
home;
irisrequired 20120601;

%% Create artificial data to be reported
%
% The tseries object will be repeatedly used in the tables below.

x = tseries(yy(2010):yy(2016),@rand);

%% Define graphics style
%
% for future use in some of the graphs.

sty = struct();
sty.axes.fontsize = 28;
sty.line.lineWidth = 2;

%% Start new report

r = report.new('','dateformat=','YYYY');

%% Start new align object
%
% The input arguments in `align` specify the total number of objects
% (following this `align` command) that will be included and aligned, and
% the number of objects that will apear on one row (i.e. the number of
% ``columns'').
%
% Here we enter 2 and 2 meaning that we include two objects that follow the
% `align` command (i.e. the table 

r.align('',2,2);

r.table({'Growth','Red subtitle'}, ...
    'range=',yy(2010):yy(2016),'captiontypeface=',{Inf,'\color{red}'});

    r.series('Series 1',x);
    r.series('Series 2',x);
    r.series('Series 3',x);
    r.series('Series 4',x);
    r.series('Series 5',x);

r.figure({'Growth','Black subtitle'},'style',sty);
    r.graph('');
    r.series('Series',x);

%% Start another align object with shared caption
%
% This one is identical to the previous one, but the table and the figure
% have identical captions (i.e. both titles and subtitles). In that case,
% only one caption is displayed centred above the aligned objects.

r.align('',2,2);

    r.table({'Growth','Shared title and subtitle'}, ...
        'range=',yy(2010):yy(2016),'captiontypeface=',{Inf,'\color{red}'});

        r.series('Series 1',x);
        r.series('Series 2',x);
        r.series('Series 3',x);
        r.series('Series 4',x);
        r.series('Series 5',x);

    r.figure({'Growth','Shared title and subtitle'}, ...
        'style=',sty);
    
        r.graph('');
        r.series('Series',x);

%% More rows aligned
%
% Here, we include more than one row of aligned objects: This is controled
% by the two input arguments in the `align` function, `K` and `NCOL`. Here
% we specify 6 and 3, meaning that the following 6 objects will be included
% and organised on two rows with 3 objects on each.
%
% * <?shared?> Because the captions in all of the objects in the first row
% are identical and the option `'sharecaption=' true` in `align` (by
% default), the title (and subtitle, if any) will be printed once, centred
% over the three tables.
%
% * <?not_shared?> The two matrices in the second row also have identical
% captions, but because we add an empty object with an empty caption, the
% titles (and subtitles, if any) will not be shared. If you remove the
% empty object, the titles (and subtitles) will be shared.
        
r.pagebreak();

c = rand(4);

r.align('',6,3);

    r.matrix('Shared title for the first row of tables',c, ...
        'rownames=',{'variable 1','variable 2','variable 3','variable 4'});
        %?shared?
    r.matrix('Shared title for the first row of tables',c, ...
        'rownames=',{'variable 1','variable 2','variable 3','variable 4'});
    r.matrix('Shared title for the first row of tables',c, ...
        'rownames=',{'variable 1','variable 2','variable 3','variable 4'});

    r.matrix('Correlation',c, ...
        'rownames=',{'variable 1','variable 2','variable 3','variable 4'});
        %?not_shared?
    r.matrix('Correlation',c, ...
        'rownames=',{'variable 1','variable 2','variable 3','variable 4'});
    r.empty();
    
%% Publish report to PDF
%
% Compile the report into a PDF named `'report_aligned_stuff.pdf'`. The
% `open` command to open the PDF <?open?> may not work on some systems.

r.publish('report_aligned_stuff.pdf');

open report_aligned_stuff.pdf; %?open?

%% Help on IRIS functions used in this m-file
%
% Use either `help` to display help in the command window, or `idoc`
% to display help in an HTML browser window.
%
%    help report
%    help report/new
%    help report/align
%    help report/table
%    help report/series
%    help report/matrix
%    help report/array
%    help report/empty
%    help report/pagebreak
%    help report/publish
%    help tseries
%    help tseries/tseries
